/* ****************************************************************
   ****************************************************************
   * Com++ Professional (c) 2009 C++ World
   * http://www.cplusplusworld.com/ mailto:compp@cplusplusworld.com
   ****************************************************************
   ****************************************************************
*/

#ifndef  SOCKETUDP_H
#define  SOCKETUDP_H

//#include <sys/types.h>
//#include <sys/socket.h>

#include <iostream>
#include <cstring>
#include <string>
#include <unistd.h>
#include <fcntl.h>

#include "ComPlusPlus/Socket.h"

namespace compp {

  /**
     Udp Socket Class. Handling connection oriented processing.
  */

  //enum IPADDRVERS { IPADDRV4, IPADDRV6 }  ;

  //const std::string SocketANYHOST = "0.0.0.0";

  class SocketUdp : public Socket {

  private:
    void NewUdpSocket();

  protected:
    std::string  SocketAddress ;
    int SocketPort;

    // SocketUdp Options

  public:
    ~SocketUdp();
    SocketUdp();
    SocketUdp(const std::string & Addr, int port);
    SocketUdp( int port);
    SocketUdp ( struct sockaddr_in *saddr, int len, int fd ) ;
    
    // Do an explicit bind
    void Bind ( const std::string & addr, int port);
    void Bind ( const int port );

    bool Connect( const std::string &addr, const int port );

    void SetSockOptBroadcast ( bool yesno ) ;
    void SetSockOptJoinMulticastGroup ( const std::string & group ) ;
    void SetSockOptWithdrawMulticastGroup ( const std::string & group ) ;
    void SetSockOptMulticastLoop ( bool val ) ;
    void SetSockOptMulticastTTL ( int val ) ;

    int Write ( char *buf, int count ) ;
    int Write ( const std::string & buf ) ;
    int Writen ( const char *buf, int count ) ;
    int Writen ( const std::string & buf ) ;

    int Sendto(const void *buf, size_t len, const std::string &host, int port);
    int Sendto(const void *buf, size_t len, const struct sockaddr_in &ad );

    int Read ( char *buf, int count, std::string &host, int & port ) ;
    int Read ( std::string &buf, int count, std::string &host, int & port ) ;
    int Read ( std::string &buf, std::string &host, int & port ) ;
	
    int Readn ( char *buf, int count ) ;
    int Readn ( std::string &buf, int count) ;

    int Recvfrom(void *buf, int len, int flags, std::string &from, int *port);

  }; 
}
#endif


